<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Role extends MY_Luckydog
{
	public function __construct()
	{
		parent::__construct();
        $this->load->model('luckydog/Role_model', 'role');
        $login_status = $this->login_status = $this->check_login_status();
        if (empty($login_status)){
            redirect('/luckydog/login');
        }
	}
    public function rolelist()
    {
        $_SESSION['v_menu'] = 'role';
        $role_list = $this->role->getRolelList();
        $power_arr = array();
        $power_config = $this->config->item("power");
        foreach($role_list as $k=>$row_first){
            foreach(json_decode($row_first['power'],true) as $row_second){
                foreach($row_second as $row_third){
                    foreach($row_third as $key=>$role_true){
                        $role_list[$k]['power_str'] .=  $power_config[$role_true]." ";
                    }
                }
            };

        }
        $data = array(
            'role_list'=>$role_list
        );

        $this->load->view('/luckydog/systemoption/role_list',$data);
    }

    public function toRolePower(){
        $id = $this->input->get("id",true) ? $this->input->get("id",true) : 0;
        $data = array();
        if(!empty($id)){
            $powerInfo = $this->role->getPowerInfo($id);
            $data['powerInfo'] = json_decode($powerInfo['power'],true);
            $data['groupID'] = $id;
            $data['name'] = $powerInfo['name'];
        }
        $this->load->view('/luckydog/systemoption/role_power',$data);
    }

    //角色控制
    public function SystemPowerSet(){
        $gropuID = $this->input->post("gropuID",true);
        $str = $this->input->post("powerstr",true);
        $rolename = $this->input->post("rolename",true);
        $info = '';
        if(!empty($str)){
            $arr = explode('|',$str); // 拆分最外围的数组
            foreach($arr as $v ){
                $arr2 = array();
                if( -1 < strpos( $v, '#')){
                    $arr2 = explode('#',$v);
                }else{
                    $arr2[] = $v;
                }
                foreach($arr2 as $v2 ){
                    $newarr3 = array();
                    $arr5 = array();
                    $arr4 = array();
                    $arr3 = array();
                    $arr3 = explode(':', $v2);
                    $arr4 = explode("-", $arr3[1]);
                    foreach($arr4 as $v4){
                        $arr5 = explode('*', $v4);
                        $newarr3[$arr5['0']] = explode('%', $arr5[1]);
                    }
                    $newarr2[$arr3[0]] = $newarr3;
                }
                $info = $newarr2;
            }
        }

        if($gropuID){
            $data = array(
                "name"=>$rolename,
                "power"=>json_encode($info)
            );
            $this->role->setSystemPower($gropuID,$data);
        }else{
            $data = array(
                "name"=>$rolename,
                "power"=>json_encode($info),
                "addtime"=>time()
            );
            $this->role->setSystemPower("",$data);
        }

        redirect('/luckydog/role/rolelist');
    }

    //删除角色
    public function delRole(){
        $id = $this->input->post("id",true);
        $res = $this->role->delRolebyid($id);
        echo $res;
    }
    //设置角色启用状态
    public function setStatus(){
        $id = $this->input->post("id",true);
        $status = $this->input->post("status",true) ? $this->input->post("status",true) :-1;
        $res = $this->role->setStatus($id,$status);
        echo $res;
    }

}
/* End of file index.php */
/* Location: ./application/controllers/luckydog/role.php */